package com.core.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.core.dao.impl.CityDAOImpl;
import com.core.dao.impl.CountryDAOImpl;
import com.core.dao.impl.StateDAOImpl;
import com.core.entities.City;
import com.core.entities.Country;
import com.core.entities.State;
import com.core.gui.components.PlaceComboBox;

public class RegistrationDialog2 extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel contentPanel = new JPanel();
	private JLabel countryLabel = new JLabel("Country:");
	private PlaceComboBox<Country> countryComboBox = new PlaceComboBox<Country>();
	private JLabel sateLabel = new JLabel("State:");
	private PlaceComboBox<State> stateComboBox = new PlaceComboBox<State>();
	private JLabel cityLabel = new JLabel("City:");
	private PlaceComboBox<City> cityComboBox = new PlaceComboBox<City>();

	// private JSeparator separator = new JSeparator();

	public RegistrationDialog2() {
		initComponents();
		initPanel();
	}

	private void initComponents() {
		try {

			countryComboBox.setPlaces(CountryDAOImpl.getInstance().findAll());
		} catch (Exception e) {
			e.printStackTrace();
		}
		countryComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Country country = (Country) countryComboBox.getModel()
						.getSelectedItem();
				countryComboBoxAction(country);
			}
		});

		stateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (stateComboBox.getModel().getSelectedItem() != null) {
					State state = (State) stateComboBox.getModel()
							.getSelectedItem();
					stateComboBoxAction(state);
				}
			}
		});
		cityComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (cityComboBox.getModel().getSelectedItem() != null) {
					City city = (City) cityComboBox.getModel()
							.getSelectedItem();
					cityComboBoxAction(city);
				}
			}
		});
	}

	private void countryComboBoxAction(Country country) {
		try {
			cityComboBox.clear();
			stateComboBox.clear();
			stateComboBox.setPlaces(StateDAOImpl.getInstance()
					.findByCountry_id(country.getId()));
			stateComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void stateComboBoxAction(State state) {
		try {
			cityComboBox.clear();
			cityComboBox.setPlaces(CityDAOImpl.getInstance().findByState_id(
					state.getId()));
			cityComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void cityComboBoxAction(City city) {

	}

	private void initPanel() {
		contentPanel.setLayout(new GridLayout(3, 2));

		contentPanel.add(countryLabel);
		contentPanel.add(countryComboBox);

		contentPanel.add(sateLabel);
		contentPanel.add(stateComboBox);

		contentPanel.add(cityLabel);
		contentPanel.add(cityComboBox);

		this.setLayout(new BorderLayout());
		this.add(contentPanel, BorderLayout.CENTER);
		this.pack();
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		try {
			RegistrationDialog2 dialog = new RegistrationDialog2();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
